<?php

namespace app\modules\user\controllers;

use yii;
use yii\web\Controller;
use common\models\Tools;
use common\models\Region;
use common\models\Store;
/**
 * 商家入驻页面
 */
class SellerController extends Controller
{
    public $enableCsrfValidation=false;
	public $leftmenu;
    /**
     * 商家入驻页面
     * @return string
     */
    public function actionRequest()
    {
        //检测登录情况
        if (Yii::$app->user->isGuest) {
            return $this->redirect('/site/login?returnurl=/user/seller/request');
        }
        $this->getView()->title = "平台入驻";
        $this->layout = "/personal_open";
        return $this->render('request',[]);
    }
	
    public function actionApply()
    {
        //检测登录情况
        if (Yii::$app->user->isGuest) {
            return $this->redirect('/site/login?returnurl=/user/seller/request');
        }
        
        $userid = Yii::$app->user->identity->id;
        $model = Store::getStoreByUserid($userid);
        if(!empty($model)){
            $type_name = '';
            if($model['type'] == 1){
                $type_name = '区域代理商';
            }else if($model['type'] == 2){
                $type_name = '普通商家';
            }
            $model['province'] = Region::getRegionNameById($model['region_1']);
            $model['city'] = Region::getRegionNameById($model['region_2']);
            $model['county'] = Region::getRegionNameById($model['region_3']);
            
            $status_arr = array(0=>'待审核',1=>'已通过',2=>'未通过');
            
            return $this->render('apply_result',['model'=>$model,'type_name'=>$type_name,'status_arr'=>$status_arr]);
        }

        $type = Tools::GetSafeParam('type', 0, 0);
        $type_name = '';
        if($type == 1){
            $type_name = '区域代理商';
        }else if($type == 2){
            $type_name = '普通商家';
        }

        $this->getView()->title = $type_name."完善资料";

        //一级地域选择
        $region_1 = Region::getFirstRegions();

        $this->layout = "/personal_open";
        return $this->render('apply',['type'=>$type,'type_name'=>$type_name,'region_1'=>$region_1]);
    }
        
        
    public function actionApplySave(){
        if (Yii::$app->user->isGuest) {
            $output['success'] = 0;
            $output['msg'] = '请先登录！';
            $output['errocode'] = 1000;
            Tools::outputjson($output);
        }
        $param = [];
        //{"name":"阿斯蒂芬","region_1":"25","region_2":"299","region_3":"2882","address":"测试地址","bussness_no":"sadf234523asdfasertadfg",
        //"bussiness_img":"/userauth\\2020-07-16/9342\\d1ce2e9027b07daea3c00d1146a61678.jpg","liaison":"dasd",
        //"liaison_id_no":"2345213412342","liaison_job":"sdf","liaison_mobile":"sdf","liaison_email":"sadf",
        //"shop_info":"sdf","shop_logo":"/userauth\\2020-07-16/2661\\ee982af848fd39dbbdc6e43861b48919.jpg"}
        $param['name'] = Tools::GetSafeParam('name', '', 1);
        $param['region_1'] = Tools::GetSafeParam('region_1', 0, 0);
        $param['region_2'] = Tools::GetSafeParam('region_2', 0, 0);
        $param['region_3'] = Tools::GetSafeParam('region_3', 0, 0);
        $param['address'] = Tools::GetSafeParam('address', '', 1);
        $param['bussiness_no'] = Tools::GetSafeParam('bussiness_no', '', 1);
        $param['bussiness_img'] = Tools::GetSafeParam('bussiness_img', '', 1);
        $param['liaison'] = Tools::GetSafeParam('liaison', '', 1);
        $param['liaison_id_type'] = Tools::GetSafeParam('liaison_id_type', 0, 0);
        $param['liaison_id_no'] = Tools::GetSafeParam('liaison_id_no', '', 1);
        $param['liaison_job'] = Tools::GetSafeParam('liaison_job', '', 1);
        $param['liaison_mobile'] = Tools::GetSafeParam('liaison_mobile', '', 1);
        $param['liaison_email'] = Tools::GetSafeParam('liaison_email', '', 1);
        $param['shop_info'] = Tools::GetSafeParam('shop_info', '', 1);
        $param['shop_logo'] = Tools::GetSafeParam('shop_logo', '', 1);
        $param['type'] = Tools::GetSafeParam('type', 0, 0);

        if($param['name']==""){
            $output['success'] = 0;
            $output['msg'] = '公司名称不能为空！';
            $output['errocode'] = 1001;
            Tools::outputjson($output);
        }
        if($param['region_1']==""){
            $output['success'] = 0;
            $output['msg'] = '请选择省！';
            $output['errocode'] = 1001;
            Tools::outputjson($output);
        }
        if($param['region_2']==""){
            $output['success'] = 0;
            $output['msg'] = '请选择市！';
            $output['errocode'] = 1001;
            Tools::outputjson($output);
        }
        if($param['region_3']==""){
            $output['success'] = 0;
            $output['msg'] = '请选择区！';
            $output['errocode'] = 1001;
            Tools::outputjson($output);
        }
        if($param['address']==""){
            $output['success'] = 0;
            $output['msg'] = '请输入详细地址！';
            $output['errocode'] = 1001;
            Tools::outputjson($output);
        }
        if($param['bussiness_no']==""){
            $output['success'] = 0;
            $output['msg'] = '请输入营业执照注册号！';
            $output['errocode'] = 1001;
            Tools::outputjson($output);
        }
        if($param['bussiness_img']==""){
            $output['success'] = 0;
            $output['msg'] = '请上传营业执照扫描件！';
            $output['errocode'] = 1001;
            Tools::outputjson($output);
        }
        if($param['liaison']==""){
            $output['success'] = 0;
            $output['msg'] = '请输入联系人姓名！';
            $output['errocode'] = 1001;
            Tools::outputjson($output);
        }
        if($param['liaison_id_no']==""){
            $output['success'] = 0;
            $output['msg'] = '请输入联系人证件号码！';
            $output['errocode'] = 1001;
            Tools::outputjson($output);
        }
        if($param['liaison_job']==""){
            $output['success'] = 0;
            $output['msg'] = '请输入联系人职务！';
            $output['errocode'] = 1001;
            Tools::outputjson($output);
        }
        if($param['liaison_mobile']==""){
            $output['success'] = 0;
            $output['msg'] = '请输入联系人手机号码！';
            $output['errocode'] = 1001;
            Tools::outputjson($output);
        }
        if($param['shop_info']==""){
            $output['success'] = 0;
            $output['msg'] = '请输入公司简介！';
            $output['errocode'] = 1001;
            Tools::outputjson($output);
        }
        if($param['shop_logo']==""){
            $output['success'] = 0;
            $output['msg'] = '请上传公司logo！';
            $output['errocode'] = 1001;
            Tools::outputjson($output);
        }
        if($param['type']==""){
            $output['success'] = 0;
            $output['msg'] = '入驻类型错误！';
            $output['errocode'] = 1001;
            Tools::outputjson($output);
        }

        $userid = Yii::$app->user->identity->id;
        $username = Yii::$app->user->identity->username;

        $result = \common\models\Store::applysave($userid,$username,$param);
        if($result == true){
            $output['success'] = 1;
            $output['msg'] = '提交成功！';
            Tools::outputjson($output);
        }else{
            $output['success'] = 0;
            $output['msg'] = '系统错误！';
            $output['errocode'] = 1002;
            $output['errodata'] = $result;
            Tools::outputjson($output);
        }
    }
}
